﻿using System;
using System.IO;
using Android.App;
using Android.Runtime;
using MuseumsBC.DataAccess;
using MuseumsBC.Models;
using Newtonsoft.Json;

namespace MuseumsBC
{
    [Application]
    public class MuseumsBCApplication : Application
    {
        public IMuseumRepository MuseumRepository { get; set; }
        private MuseumData _MuseumData { get; set; }

        public MuseumsBCApplication(IntPtr handle, JniHandleOwnership transfer)
            : base(handle, transfer)
        {
        }

        public override void OnCreate()
        {
            base.OnCreate();

            //MuseumRepository = new StandardMuseumRepository(this);
            MuseumRepository = new OrmMuseumRepository(this);


            // initial data load

            if (MuseumRepository.GetAllMuseums().Count == 0)
            {
                _MuseumData = LoadMuseumsJson();
                foreach (Museum museum in _MuseumData.Museums)
                {
                    this.MuseumRepository.AddMuseum(museum);
                }

            }



            /*
             * some good db default creation info:
             * http://stackoverflow.com/questions/4938867/need-an-example-of-sqlite-with-monodroid
             * 
             * 
             * However, to use the following code you must be targeting Android 2.2 or later to use Mono.Data.Sqlite. If you need to target an earlier Android version, you should look into a fully managed replacement, such as managed-sqlite.

                Furthermore, this example is using Mono.Data.Sqlite.dll, which is included in the MonoDroid SDK.

                First, edit your project assembly references and add a reference for Mono.Data.Sqlite.dll and System.Data.dll.

                Second, within your source code, add:

                using System.Data;
                using Mono.Data.Sqlite;
                Finally, use ye normal ADO.NET code:

                string dbPath = Path.Combine (
                        Environment.GetFolderPath (Environment.SpecialFolder.Personal),
                        "items.db3");
                bool exists = File.Exists (dbPath);
                if (!exists)
                    SqliteConnection.CreateFile (dbPath);
                var connection = new SqliteConnection ("Data Source=" + dbPath);
                connection.Open ();
                if (!exists) {
                    // This is the first time the app has run and/or that we need the DB.
                    // Copy a "template" DB from your assets, or programmatically create one.
                    var commands = new[]{
                        "CREATE TABLE [Items] (Key ntext, Value ntext);",
                        "INSERT INTO [Items] ([Key], [Value]) VALUES ('sample', 'text')"
                    };
                    foreach (var command in commands) {
                        using (var c = connection.CreateCommand ()) {
                            c.CommandText = command;
                            c.ExecuteNonQuery ();
                        }
                    }
                }
                // use `connection`...
                // here, we'll just append the contents to a TextView
                using (var contents = connection.CreateCommand ()) {
                    contents.CommandText = "SELECT [Key], [Value] from [Items]";
                    var r = contents.ExecuteReader ();
                    while (r.Read ())
                        MyTextView.Text += string.Format ("\n\tKey={0}; Value={1}",
                                r ["Key"].ToString (), r ["Value"].ToString ());
                }
                connection.Close ();
             * */


        }

        private MuseumData LoadMuseumsJson()
        {
            Stream stream = Assets.Open(@"museumsJSON.js");
            StreamReader reader = new StreamReader(stream);

            string museumsJson = reader.ReadToEnd();
            //museumsJson = Models.DummyJson.MuseumsJson();

            var museums = JsonConvert.DeserializeObject<MuseumData>(museumsJson);

            return museums;
        }

    }

}
